package es.unileon.is.finalproyect.userinterface;

import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

import es.unileon.is.finalproyect.adminInterface.AdminInterface;
import es.unileon.is.finalproyect.themes.ChangeTheme;
import es.unileon.is.finalproyect.themes.Themes;
import es.unileon.is.finalproyect.users.User;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;


import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JPasswordField;


import javax.swing.JLabel;

import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import com.nilo.plaf.nimrod.NimRODTheme;


import oracle.net.aso.a;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class LoginInterface extends JFrame {
	
	private JPanel contentPane;
	private JTextField txtUser;
	private String usuario= new String();
	private String password = new String();
	private JPasswordField pwdPassword;
	private JTextField lblPassword;
	private JMenuItem help;
	private JMenuBar menuBar;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginInterface frame = new LoginInterface();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginInterface() {

		//Definimos el frame.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(10, 10, 400, 130);
		setSize(450,150);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//*****modificaciones para JAVAHELP*******
		menuBar = new JMenuBar();
		JMenu menuHelp = new JMenu("Help");
		help = new JMenuItem("Show help");
		//*********** MODIFICACIONES THEME *******************
		JMenu menuthemes = new JMenu("Themes");
		menuthemes = Themes.showNewTheme(menuthemes, getContentPane());
		menuBar.add(menuthemes);
		//****************************************************
		menuBar.add(Box.createHorizontalGlue());
		menuBar.add(menuHelp);
		menuHelp.add(help);
		setJMenuBar(menuBar);
		//metodo para dar la accion a help
		ViewHelp.showHelp(help, getContentPane(), "ventana_loggin");
		//*****************************************
		
		
		//botones y textfield del frame principal
		txtUser = new JTextField();
		txtUser.setText("User");
		txtUser.setBounds(10, 28, 86, (int)txtUser.getPreferredSize().getHeight());
		contentPane.add(txtUser);
		txtUser.setColumns(10);
		txtUser.addFocusListener(new FocusAdapter(){
			public void focusGained(FocusEvent evt){
				((JTextField)evt.getSource()).selectAll();
			}
		});
		
		lblPassword = new JTextField("Password");
		lblPassword.setBounds(106, 28, 86, (int)lblPassword.getPreferredSize().getHeight());
		contentPane.add(lblPassword);
		pwdPassword = new JPasswordField();
		pwdPassword.setToolTipText("Password");
		lblPassword.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				pwdPassword.setBounds(106, 28, 86, (int)lblPassword.getPreferredSize().getHeight());
				getContentPane().remove(lblPassword);
				contentPane.add(pwdPassword);
			}
		});
		
		JLabel lblWantAnAccount = new JLabel("Want an account?");
		lblWantAnAccount.setBounds(232, 61, (int)lblWantAnAccount.getPreferredSize().getWidth(), (int)lblWantAnAccount.getPreferredSize().getHeight());
		contentPane.add(lblWantAnAccount);
		
		
		JButton btnLogin = new JButton("Login");
		btnLogin.setBounds(232, 27, (int)btnLogin.getPreferredSize().getWidth(), (int)btnLogin.getPreferredSize().getHeight());
		contentPane.add(btnLogin);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new RegisterInterface().setVisible(true);
			}
		});
		btnRegister.setBounds(345, 60, (int)btnRegister.getPreferredSize().getWidth(), (int)btnRegister.getPreferredSize().getHeight());
		contentPane.add(btnRegister);
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				usuario=txtUser.getText();
				password=String.valueOf(pwdPassword.getPassword());
				User u = Access.Login(usuario,password);
				if(u == null){
					//Lanzar mensaje de error de que el usuario y contrasenya es incorrecto.
					JOptionPane.showMessageDialog(getContentPane(), "The user and/or password are incorrect", "Error", JOptionPane.ERROR_MESSAGE);
				}
				else if(u.isPending()){
						//lanzar un messageOption diciendo que el usuario aun no ha sido admitido, que lo intente mas tarde
						JOptionPane.showMessageDialog(getContentPane(), "The user is not yet in our system, please wait an admin accept you.", "Error", JOptionPane.ERROR_MESSAGE);
					}
				else{
					dispose();
					new UserInterface(u).setVisible(true);
					if(u.isAdministrator()){
						new AdminInterface(u).setVisible(true);
					}
				}
			}
		});			
	}
}
